<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/4/9 0009
 * Time: 20:50
 */
ini_set('memory_limit','256M');
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');


class Jiankang_info extends CI_Controller
{

    public function video_list()
    {
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        $id = 71;
        $info = $this->db->query("select * from ai_spider_39_disease where id=".$id)->row_array();
        $url = "https://www.99.com.cn/jibing/getlist.json";
        $pages = 99;
        foreach($pages as $p){
//            $data['page'] = $p;
//            $data['type'] = "video";
//            $data['id'] = $info['did'];
//            $data['id'] = $info['did'];
//            $data['_token'] = "uJ0fs1Ezih22kJh4W8UTcDHcmAY2a0BwQ55gWuVQ";
            $data = "page=".$p."&type=video&id=".$info['did'];
            $content = http_post($url,$data);
            dd($content);
            echo $content;exit;
        }


    }

    public function info(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $urlid = $redis->lpop('jiankang_video_list');
        //$urlid = 1;
        if(!$urlid){
            echo "nodata";
            exit;
        }
        $url_info = $this->db->query("select id,disease_id,vid,v_url from ai_39_disease_video where id=".$urlid)->row_array();
        $disease_id = $url_info['disease_id'];

        $disease_info = $this->db->query("select disease_name from ai_spider_39_disease where did=".$disease_id)->row_array();
        $disease_name = $disease_info['disease_name'];

        //echo "采集开始:".time()."-";
        $detail_url = "https://www.99.com.cn/shipin/".$url_info['vid'].".html";
        //echo $detail_url;
        $content = geturl($detail_url);
        $html = str_get_html($content);
        // 处理对应字段
        $video_name = trim($html->find("h1",0)->innertext);
        $video_url = $url_info['v_url'];
        $video_content = trim($html->find("div[class=atcle-cont]",0)->innertext);
        $video_content = str_replace('<h3>视频内容</h3>','',$video_content);
        $doctor_name = $html->find("dd",0)->find('b',0)->find('a',0)->innertext;
        $doctor_title = $html->find("dd",0)->find('b',0)->find('span',0)->innertext;
        $hospital = $html->find(".hospitaln",0)->innertext;
        $department = $html->find("dd",0)->find('p',0)->find('font',0)->innertext;
        $video_urls = explode("/",$video_url);
        $video_url_local = 'jiankang/'.$video_urls[5];

        // 下载mp4,研究是用oss接口上传
        $this->load->library('aliyun_oss');
        $save_dir = "D:\youlai_video";
        //echo "下载开始:".time()."-";
        $video_url = str_replace("//files.","https://files.",$video_url);
        $r = $this->getMp4($video_url,$save_dir.'/',md5($video_name).$url_info['id'].'.mp4');
        //echo "下载结束:".time()."-";
        if($r){
            // 填写Bucket名称，例如examplebucket。
            $bucket= "rzaidata";
// 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
            $object = "jiankang/".md5($video_name).$url_info['id'].'.mp4';
// 填写待上传的字符串。
            $file_name = $save_dir.'/'.md5($video_name).$url_info['id'].'.mp4';
            //echo "上传开始:".time()."-";
            $res = $this->aliyun_oss->uploadFile($bucket, $object, $file_name);
            //echo "上传结束:".time()."-";
            if($res["info"]['http_code'] == 200){
                $video_url_local = $object;
                // 移除本地文件
                unlink($file_name);
                // 拼接sql
                $sql = "INSERT ignore INTO ai_spider_39_disease_video(disease_name,disease_id,video_name,doctor_name,doctor_title,hospital,department,video_url,content,video_old_url,vid) VALUES ('".$disease_name."','".$disease_id."','".$video_name."','".$doctor_name."','".$doctor_title."','".$hospital."','".$department."','".$video_url_local."','".$video_content."','".$url_info['v_url']."','".$url_info['vid']."')";
                //echo $sql;exit;
                $this->db->query($sql);
                //echo "入库结束:".time()."-";
            }else{
                $redis->rPush('jiankang_video_list',$urlid);
            }
        }else{
            $redis->rPush('jiankang_video_list',$urlid);
        }
        echo 1;
        //$ = $html->find("ul[id=videoList] li");
    }

    function getMp4($url,$save_dir='',$filename='',$refer=''){
        if(trim($url)==''){
            return -1;
        }
        if(trim($save_dir)==''){
            $save_dir='./';
        }
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
        if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
            return -3;
        }
        //获取远程文件所采用的方法
        //if($type){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //头部要送出'Expect: '
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名
        $img = curl_exec($ch);
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($statusCode == 200 && $img){
            $fp2=@fopen($save_dir.$filename,'a');
            fwrite($fp2,$img);
            fclose($fp2);
            unset($img,$url);
            return 1;
        }else{
            return 0;
        }

    }


    public function push(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        $list = $this->db->query("select id from ai_39_disease_video")->result_array();
        foreach($list as $vo){
            $redis->rPush('jiankang_video_list',$vo['id']);
        }
    }

    public function test(){
        $url = "https://files.99.com.cn/video/202004/20042807553545.mp4";
        $context = stream_context_create(array('http'=>array('ignore_errors'=>true)));
        $contents = file_get_contents($url, FALSE, $context);
        echo $contents;
    }




}